package com.syqc.comlib.utils
import java.text.Collator
import java.util.*

object ArrayUtil {
    val collator: Collator = Collator.getInstance(Locale.CHINA)
    private val comparator: Comparator<Any?> = Collator.getInstance(Locale.CHINA)
    private val CHINA = arrayOf(
        arrayOf("零", "10"),
        arrayOf("一", "11"),
        arrayOf("二", "12"),
        arrayOf("三", "13"),
        arrayOf("四", "14"),
        arrayOf("五", "15"),
        arrayOf("六", "16"),
        arrayOf("七", "17"),
        arrayOf("八", "18"),
        arrayOf("九", "19"),
        arrayOf("十", "20")
    )

    fun <T> sortBy(list: Collection<T>, field: String, asc: Boolean): Collection<T> {
        if (list.isEmpty()) {
            return list
        }
        Collections.sort(list as MutableList<Any>, Comparator { o1: Any, o2: Any ->
            var v1: String? = null
            var v2: String? = null
            var flag = 0
            try {
                if (o1 is Map<*, *>) {
                    v1 = o1[field].toString()
                    v2 = (o2 as Map<*, *>)[field].toString()
                } else {
                    val clazz: Class<*> = o1.javaClass
                    val firstLetter = field.substring(0, 1).uppercase(Locale.getDefault())
                    //拼接属性get方法的方法名
                    val getMethodName = "get" + firstLetter + field.substring(1)
                    //现在动态获取get方法

                    val getmethod = clazz.getDeclaredMethod(getMethodName)
                    v1 = getmethod.invoke(o1) as String
                    v2 = getmethod.invoke(o2) as String
                }
                flag = (if (asc) 1 else -1) * compare(v1, v2)
            } catch (e: Exception) {

            }
            flag
        } as Comparator<Any>)
        return list
    }

    //Double 类型排序
    fun <T> sortDoubleBy(list: Collection<T>?, field: String, asc: Boolean): Collection<T>? {
        if (list.isNullOrEmpty()) {
            return list
        }
        Collections.sort(list as MutableList<Any>, Comparator { o1: Any, o2: Any ->
            var v1: Double? = null
            var v2: Double? = null
            var flag = 0
            try {
                if (o1 is Map<*, *>) {
                    v1 = o1[field] as Double?
                    v2 = (o2 as Map<*, *>)[field] as Double?
                } else {
                    val clazz: Class<*> = o1.javaClass
                    val firstLetter = field.substring(0, 1).uppercase(Locale.getDefault())
                    //拼接属性get方法的方法名
                    val getMethodName = "get" + firstLetter + field.substring(1)
                    //现在动态获取get方法
                    val getmethod = clazz.getDeclaredMethod(getMethodName)
                    v1 = getmethod.invoke(o1) as Double
                    v2 = getmethod.invoke(o2) as Double
                }
                flag = (if (asc) 1 else -1) * v1!!.compareTo(v2!!)
            } catch (e: Exception) {

            }
            flag
        } as Comparator<Any>)
        return list
    }

    fun <T> sortIntBy(list: Collection<T>, field: String, asc: Boolean): Collection<T> {
        if (list.isNullOrEmpty()) {
            return list
        }
        Collections.sort(list as MutableList<Any>, Comparator { o1: Any, o2: Any ->
            var v1: Int? = null
            var v2: Int? = null
            var flag = 0
            try {
                if (o1 is Map<*, *>) {
                    v1 = o1[field] as Int?
                    v2 = (o2 as Map<*, *>)[field] as Int?
                } else {
                    val clazz: Class<*> = o1.javaClass
                    val firstLetter = field.substring(0, 1).uppercase(Locale.getDefault())
                    //拼接属性get方法的方法名
                    val getMethodName = "get" + firstLetter + field.substring(1)
                    //现在动态获取get方法
                    val getmethod = clazz.getDeclaredMethod(getMethodName)
                    v1 = getmethod.invoke(o1) as Int
                    v2 = getmethod.invoke(o2) as Int
                }
                flag = (if (asc) 1 else -1) * v1!!.compareTo(v2!!)
            } catch (e: Exception) {

            }
            flag
        } as Comparator<Any>)
        return list
    }

    // 如果是数字，按数字排序，否则按字符串排序
    private fun compare(v1: String?, v2: String?): Int {

        if (v1.isNullOrEmpty() || v2.isNullOrEmpty())
            return comparator.compare(v1, v2)

        val i1 = isNum(v1)
        val i2 = isNum(v2)
        if (i1 && i2) return v1.toLong().compareTo(v2.toLong())
        if (i1) return -1
        if (i2) return 1
        var value1 = v1
        var value2 = v2
        for (vs in CHINA) {
            value1 = v1.replace(vs[0], vs[1])
            value2 = v2.replace(vs[0], vs[1])
        }
        return comparator.compare(value1, value2)
    }

    fun isNum(value: String): Boolean {
        return try {
            value.toLong()
            true
        } catch (e: Exception) {
            false
        }
    }

    fun isEmpty(str: String?): Boolean {
        if (str.isNullOrEmpty() || "null".equals(str, ignoreCase = true)) return true

        for (element in str) {
            if (element != ' ' && element != '\t' && element != '\r' && element != '\n') {
                return false
            }
        }
        return true
    }
}